const mixin = require('../../mixin/mixin')
var app = getApp();
import util from '../../utils/util.js';
import { getBooksListByGradeId4mini } from '../../api/exhibitionBookConfigAPI'
import { configApp } from '../../utils/config'
const FN = require('../../utils/public');

Page({
  mixins: [mixin],
  /**
   * 页面的初始数据 
   */
  data: {
    gradeId: null,
    page: 1,
    pageSize: 150,
    bookList: [],
    adObj:{},
    gradeArr: [],
    currGrade: '()',
    show: false,
    booksEditions: [configApp.booksEdition],
    semesterIndex: -1,// 学期
    gradeActiveIndex: -1,// 年级 
    booksEditionIndex: 0,// 教程版本
    evadingAudit: {}
  },

  /**
   * 生命周期函数--监听页面加载 
   */
  onLoad: function (options) {
    var that = this;
    this.adInit();
    app.callback = res => {
      that.setData({
        evadingAudit: res
      })
    }
    that.getBooks();
  },

  adInit() {
    var that = this;
    var pageUrl = util.getCurrentPageUrl()
    var item = app.globalData.advertisement.find(obj => obj.advertisementPage === pageUrl)
      that.setData({
        adObj: item
      })
  },

  replayData() {
    var that = this;
    wx.removeStorage({
      key: 'X-Access-Token',
      success(res) {
        console.log(res)
        that.getBooks();
      }
    })
  },

  study: function (e) {
    var booksid = e.currentTarget.dataset.booksid;
    wx.navigateTo({
      url: '../myBooksChapter/myBooksChapter?booksId=' + booksid
    })
  },

  // 获取书本列表
  getBooks: function () {
    var that = this, obj = this.setSearchParms();
    getBooksListByGradeId4mini({
      pageNum: this.data.page,
      pageSize: this.data.pageSize,
      ...obj
    }).then(res => {
      if (res.code == '200') {
        if (res.data.list.length == 0) {
          FN.Toast("没有更多的数据了！")
        }
        that.setData({
          bookList: res.data.list
        })
      }
    })

  },

  setSearchParms() {
    var obj = {}, that = this;
    if (that.data.semesterIndex == -1) {
      obj.booksSemester = ""
    } else {
      obj.booksSemester = that.data.semesterIndex == 0 ? '上学期' : '下学期'
    }

    if (that.data.gradeActiveIndex == -1) {
      obj.gradeId = ""
    } else {
      obj.gradeId = that.data.currGrade == '()' ? '' : that.data.gradeArr[that.data.gradeActiveIndex].gradeId
    }

    if (that.data.booksEditionIndex == -1) {
      obj.booksEdition = ""
    } else {
      obj.booksEdition = that.data.booksEditions[that.data.booksEditionIndex]
    }
    return obj;
  },

  // 图片加载完毕
  imgLoaded: function (e) {
    const arr = this.data.bookList, i = e.currentTarget.dataset.index, newArr = new Array();
    arr.map(function (item, index, arr) {
      if (i == index) {
        item.loadOver = "loadOver";
      }
      newArr.push(item);
    })
    this.setData({
      bookList: newArr
    })
  },

  imageLoadEnd: function (e) {
    var index = e.currentTarget.dataset.index;
    var loadOverObj = 'bookList.[' + index + '].loadOver';
    this.setData({
      [loadOverObj]: true
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }

})